/**
 * 
 */
package com.googlecode.xcarve.coder;

import io.netty.handler.codec.UnsupportedMessageTypeException;

import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.xcarve.coder.hessian.HessianCoder;

/**
 * @author xinyun.d
 *
 */
public class CoderFactory {
	static final ConcurrentHashMap<String,Coder> coderMap = new ConcurrentHashMap<String,Coder>();
	static final Logger logger = LoggerFactory.getLogger(CoderFactory.class);
	static{
		coderMap.put("hessian", new HessianCoder());
	}
	
	public static Coder getCoder(String code){
		if(StringUtils.isBlank(code)){
			 logger.info("application not set serializable type, return default serialize is hessian.");
			 return new HessianCoder();
		}else{
			Coder coder = coderMap.get(code);
			if(coder == null){
				throw new UnsupportedMessageTypeException("unspport message codec type,code="+code);
			}else{
				return coderMap.get(code);
			}
		}
	}
}
